﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace 深入浅出WPF_BookDemo.Views
{
    /// <summary>
    /// ConvertWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ConvertWindow : Window
    {
        public ConvertWindow()
        {
            InitializeComponent();
            setMultiBinding();
        }

        private void setMultiBinding()
        {
            Binding binding1 = new Binding("Text") { Source = this.textBox1 };
            Binding binding2 = new Binding("Text") { Source = this.textBox2 };
            Binding binding3 = new Binding("Text") { Source = this.textBox3 };
            Binding binding4 = new Binding("Text") { Source = this.textBox4 };

            MultiBinding mb = new MultiBinding() { Mode = BindingMode.OneWay };
            mb.Bindings.Add(binding1);
            mb.Bindings.Add(binding2);
            mb.Bindings.Add(binding3);
            mb.Bindings.Add(binding4);
            mb.Converter = new LogonMultiBindingConverter();

            this.button1.SetBinding(Button.IsEnabledProperty, mb);

        }

        public class LogonMultiBindingConverter : IMultiValueConverter
        {
            public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
            {
                if (!values.Cast<string>().Any(text => string.IsNullOrEmpty(text))
                    && values[0].ToString() == values[1].ToString()
                    && values[2].ToString() == values[3].ToString())
                {
                    return true;
                }
                return false;
            }

            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

        private void buttonLoad_Click(object sender, RoutedEventArgs e)
        {
            List<Plane> planeList = new List<Plane>()
            {
                new Plane() { Category= Category.Bomber, Name="B-1", State = State.Unknown},
                new Plane() { Category= Category.Bomber, Name="B-2", State = State.Unknown},
                new Plane() { Category= Category.Fighter, Name="F-22", State = State.Unknown},
                new Plane() { Category= Category.Fighter, Name="Su-47", State = State.Unknown},
                new Plane() { Category= Category.Bomber, Name="B-52", State = State.Unknown},
                new Plane() { Category= Category.Fighter, Name="J-10", State = State.Unknown}
            };
            this.listBoxPlane.ItemsSource = planeList;
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (Plane p in listBoxPlane.Items)
            {
                sb.AppendLine(string.Format("Category={0}, Name={1}, State={2}", p.Category, p.Name, p.State));
            }
            File.WriteAllText(@"E:\PlaneList.txt", sb.ToString());
        }

    }

    //不要自己写接口，自己写会有问题!!!
    //public interface IValueConverter
    //{
    //    object Convert(object value, Type targetType, object parameter, CultureInfo culture);
    //    object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
    //}

    public enum Category
    {
        Bomber,
        Fighter
    }

    public enum State
    {
        Available,
        Locked,
        Unknown
    }

    public class Plane
    {
        public Category Category { get; set; }
        public string Name { get; set; }
        public State State { get; set; }
    }

    public class CategoryToSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Category category = (Category)value;
            switch (category)
            {
                case Category.Bomber:
                    return @"\Icons\Rainy.png";
                case Category.Fighter:
                    return @"\Icons\Sunny.png";
                default:
                    return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class StateToNullableBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            State s = (State)value;
            switch (s)
            {
                case State.Locked:
                    return false;
                case State.Available:
                    return true;
                case State.Unknown:
                default:
                    return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool? nb = (bool?)value;
            switch (nb)
            {
                case true:
                    return State.Available;
                case false:
                    return State.Locked;
                case null:
                default:
                    return State.Unknown;
            }
        }
    }

}
